const request = require('request')

const _impl = module.exports

_impl.send = (opts) => new Promise((resolve, reject) => {
    request(opts, (error, response, body) => {
        if (error) {
            reject(error)
        } else {
            resolve(_.isObject(body) ? body : JSON.parse( body|| '{}'))
        }
    })
})

_impl.get = (url, opts) => {
    opts = opts || {}
    opts.method = 'GET'
    opts.url = url
    return _impl.send(opts)
}

_impl.delete = (url, opts) => {
    opts = opts || {}
    opts.method = 'DELETE'
    opts.url = url
    return _impl.send(opts)
}

_impl.put = (url, data, opts) => {
    opts = opts || {}
    opts.method = 'PUT'
    opts.url = url
    opts.json = true
    opts.body = data
    return _impl.send(opts)
}

_impl.post = (url, data, opts) => {
    opts = opts || {}
    opts.method = 'POST'
    opts.url = url
    opts.json = true
    opts.body = data
    return _impl.send(opts)
}

_impl.patch = (url, data, opts) => {
    opts = opts || {}
    opts.method = 'PATCH'
    opts.url = url
    opts.json = true
    opts.body = data
    return _impl.send(opts)
}
